package com.springboot.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.dao.BookTypeMapper;
import com.springboot.entity.Book;
import com.springboot.entity.Booktype;
import com.springboot.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class BookTypeServiceImpl implements BookBTypeService {
    @Autowired
    private BookTypeMapper bookTypeMapper;
    @Override
    public ResultVO BookTypeList(String name, int pageNum) {

        PageHelper.startPage(pageNum,5); //设置分页的页面容量
        Example example=new Example(Booktype.class);
        Example.Criteria criteria=example.createCriteria();
//模糊查询
        if(!StringUtils.isEmpty(name)){
//进行模糊查询
            criteria.andLike("name",'%'+name+'%');
        }
        List<Booktype> list = bookTypeMapper.selectByExample(example);
        PageInfo<Booktype> pageInfo=new PageInfo<Booktype>(list);
        return new ResultVO(1005,"查询成功",pageInfo);
    }

    @Override
    public ResultVO addBookType(Booktype booktype) {
        int i = bookTypeMapper.insertSelective(booktype);  //选择性增加
        if(i>0){
            return new ResultVO(2000,"增加成功",i);
        }else{
            return new ResultVO(2001,"增加失败",null);
        }
    }

    @Override
    public ResultVO deleteBookType(long tid) {
        int n =bookTypeMapper.deleteType(tid);
        if(n>0){
            return new ResultVO(3000,"删除成功",n);
        }else{
            return new ResultVO(3001,"删除失败",null);
        }
    }

    @Override
    public ResultVO saveBookType(Booktype booktype) {
        int i = bookTypeMapper.updateByPrimaryKeySelective(booktype);
        if(i>0){
            return new ResultVO(3000,"修改成功",i);
        }else{
            return new ResultVO(3001,"修改失败",null);
        }
    }

}
